package com.performax.database.Dao;

import java.util.*;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.performax.database.Vo.*;
import com.performax.database.oh2game.mapper.CommunityInfoMapper;

@Service(value = "oh2gameCommunityDao")
public class Oh2GameCommunityDao {
	
	@Resource(name = "communityInfoMapper")
	private	CommunityInfoMapper communityInfoMapper;
	
	public List<NoticeInfoVo> getNoticeList(){
		return this.communityInfoMapper.getNoticeList();
	}
	
	public List<BoardInfoVo> getBoardList(HashMap<String, Object> param){
		return this.communityInfoMapper.getBoardList(param);
	}
	
	public int getBoardListCnt(HashMap<String, Object> param){
		return this.communityInfoMapper.getBoardListCnt(param);
	}
	
	public BoardDetailVo getBoardDetail(HashMap<String, Object> param){
		return this.communityInfoMapper.getBoardDetail(param);
	}
	
	public List<AttachInfoVo> getAttachFileList(int boardId){
		this.communityInfoMapper.incBoardViewCnt(boardId);
		return this.communityInfoMapper.getAttachInfo(boardId);
	}
	
	public int regBoard(HashMap<String, Object> param){
		this.communityInfoMapper.regBoard(param);
		int boardId	=	((Long)param.get("BOARD_ID")).intValue();
		int	uploadCnt	=	(Integer)param.get("uploadCnt");
	
		if(uploadCnt	>	0){
			List<AttachInfoVo> attFileInfo	=	(List<AttachInfoVo>)param.get("attFileInfo");
			for(AttachInfoVo attInfo : attFileInfo){
				this.communityInfoMapper.regAttachFile(attInfo.getOrgName(), attInfo.getUrl(), boardId);
			}
		}
		
		return boardId;
	}
	
	public void updBoard(HashMap<String, Object> param){
		this.communityInfoMapper.updBoard(param);
		
		int boardId	=	(Integer)param.get("BOARD_ID");
		
		String delFile	=	(String)param.get("delFile");
		
		if(delFile	!= null){
			String[] fileId	=	delFile.split("!");
			for(int i = 0 ; i < fileId.length ; i++){
				this.communityInfoMapper.delAttachFile(Integer.parseInt(fileId[i]));
			}
		}
		
		int	uploadCnt	=	(Integer)param.get("uploadCnt");
		if(uploadCnt	>	0){
			List<AttachInfoVo> attFileInfo	=	(List<AttachInfoVo>)param.get("attFileInfo");
			for(AttachInfoVo attInfo : attFileInfo){
				this.communityInfoMapper.regAttachFile(attInfo.getOrgName(), attInfo.getUrl(), boardId);
			}
		}
	}
	
	public void delBoard(int boardId){
		this.communityInfoMapper.delBoard(boardId);
		this.communityInfoMapper.delBoardReplyByBoardId(boardId);
	}
	
	public List<ReplyInfoVo> getBoardReplyList(HashMap<String, Object> param){
		return this.communityInfoMapper.getBoardReplyList(param);
	}
	
	public int getBoardReplyListCnt(int boardId){
		return this.communityInfoMapper.getBoardReplyCnt(boardId);
	}
	
	public List<Integer> getBoardReplyReportList(String userId, int boardId){
		return this.communityInfoMapper.getBoardReplyReportList(userId, boardId, "BR");
	}
	
	public List<Integer> getBoardReplyRecList(String userId, int boardId){
		return this.communityInfoMapper.getBoardReplyRecList(userId, boardId, "BR");
	}
	
	
	public void regBoardReply(HashMap<String, Object> param, int upperId, int boardId){
		//increase orderNo
		this.communityInfoMapper.incBoardReplyOrder(upperId);
		
		this.communityInfoMapper.regBoardReply(param);
		
		this.communityInfoMapper.setBoardReplyCnt(boardId, 1);
	}
	
	public void updBoardReply(HashMap<String, Object> param){
		this.communityInfoMapper.updBoardReply(param);
	}
	
	public void delBoardReply(int replyId){
		
		HashMap<String, Object> replyInfo	=	this.communityInfoMapper.getBoardReply(replyId);
		
		int deleteCnt	=	this.communityInfoMapper.delBoardReply(replyId, (Integer)replyInfo.get("upperId"));
		
		this.communityInfoMapper.setBoardReplyCnt((Integer)replyInfo.get("boardId"), (Math.abs(deleteCnt) * -1));
		
	}
	
	public int recommend(HashMap<String, Object> param){
		this.communityInfoMapper.recommendBoardReply(param);
		int id	=	0;
		String	target	=	(String)param.get("target");
		if(target.equals("BO")){
			id	=	(Integer)param.get("boardId");
		}else{
			id	=	(Integer)param.get("replyId");
		}
		this.communityInfoMapper.setRecCnt(target, id);
		
		return this.communityInfoMapper.getRecCnt(target, id);
	}
	


	public void report(HashMap<String, Object> param){
		this.communityInfoMapper.reportBoardReply(param);
		int id	=	0;
		String	target	=	(String)param.get("target");
		if(target.equals("BO")){
			id	=	(Integer)param.get("boardId");
		}else{
			id	=	(Integer)param.get("replyId");
		}
		this.communityInfoMapper.setReportCnt(target, id);
	}
	
	public List<FriendInfoVo> getFriendList(){
		return this.communityInfoMapper.getFriendList();
	}
	
	public void friendreg(String regType, String userId){
		String snsAccntId	=	this.communityInfoMapper.getSnsAccount(userId);
		int count	=	1;
		if(regType.equals("Y")){
			this.communityInfoMapper.regFriend(snsAccntId);
		}else{
			count	=	-1;
			this.communityInfoMapper.delFriend(snsAccntId);
		}
	}
}
